#include "ts.h"

static int ts_fd	;	//触摸屏的文件描述符 
       //int ts_touch ;	//触摸屏的触摸值
 	   int cell[9]= {0};

void *Ts_Pt (void *arg)
{
	while(1)
	{
		
		Ts_Get_xy(&ts_x, &ts_y, &ts_touch);
		if(page==page_main)
		{
			if (move_flag == 1)
			{
	  			 if(ts_x>652 && ts_x<776 && ts_y>248 && ts_y<340 && ts_touch==1 && (cell[i] !=2)) cell[0] = 1;
	 		else if(ts_x>652 && ts_x<776 && ts_y>131 && ts_y<226 && ts_touch==1 && (cell[i] !=2)) cell[1] = 1; 
	 		else if(ts_x>652 && ts_x<776 && ts_y>17  && ts_y<111 && ts_touch==1 && (cell[i] !=2)) cell[2] = 1; 
	 		else if(ts_x>480 && ts_x<603 && ts_y>17  && ts_y<111 && ts_touch==1 && (cell[i] !=2)) cell[3] = 1; 
	 		else if(ts_x>326 && ts_x<425 && ts_y>17  && ts_y<111 && ts_touch==1 && (cell[i] !=2)) cell[4] = 1;
	 		else if(ts_x>176 && ts_x<258 && ts_y>17  && ts_y<111 && ts_touch==1 && (cell[i] !=2)) cell[5] = 1;
	 		else if(ts_x>21  && ts_x<143 && ts_y>17  && ts_y<111 && ts_touch==1 && (cell[i] !=2)) cell[6] = 1;
	 		else if(ts_x>21  && ts_x<143 && ts_y>130 && ts_y<225 && ts_touch==1 && (cell[i] !=2)) cell[7] = 1;
	 		else if(ts_x>21  && ts_x<143 && ts_y>246 && ts_y<338 && ts_touch==1 && (cell[i] !=2)) cell[8] = 1;
	 		move_flag = 0;
	 		}
 		if(ts_x>21  && ts_x<143 && ts_y>362 && ts_y<564 && ts_touch==1 )
			{
				page=page_set;
				sleep(1);
				Bmp_Show(0, 0, 800, 480, "./set.bmp");
			}
		}
		else if(page==page_set) 
		{
 			if(ts_x>267  && ts_x<520 && ts_y>114 && ts_y<180 && ts_touch==1) 
 				{
 					page=page_photo;
 					Bmp_Show(0, 0, 800, 480, "./photo1.bmp");
 				}
 			else if(ts_x>267  && ts_x<520 && ts_y>220 && ts_y<285 && ts_touch==1) 
 			{
 				page=page_main;
 				setauto=0;
 				Bmp_Show(0, 0, 800, 480, "./white.bmp");
 				if (cell[0] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "1", 654, 246,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "1", 654, 246,0  ,255,0);
				if (cell[1] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "2", 654, 131,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "2", 654, 131,0  ,255,0);
				if (cell[2] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "3", 654, 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "3", 654, 16 ,0  ,255,0);
				if (cell[3] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "4", 481, 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "4", 481, 16 ,0  ,255,0);
				if (cell[4] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "5", 328, 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "5", 328, 16 ,0  ,255,0);
				if (cell[5] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "6", 176, 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "6", 176, 16 ,0  ,255,0);
				if (cell[6] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "7", 22 , 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "7", 22 , 16 ,0  ,255,0);
				if (cell[7] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "8", 22 , 132,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "8", 22 , 132,0  ,255,0);
				if (cell[8] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "9", 22 , 247,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "9", 22 , 247,0  ,255,0);
				Font_Lcd_Show(30, 125, 95, 25, 40, "操作页面",22,362,0,255,0);

 			}
 			 else if(ts_x>267  && ts_x<520 && ts_y>338 && ts_y<404 && ts_touch==1) 
 			{//自动
 				page=page_main;
 				setauto=1;
 				Bmp_Show(0, 0, 800, 480, "./white.bmp");
 				if (cell[0] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "1", 654, 246,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "1", 654, 246,0  ,255,0);
				if (cell[1] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "2", 654, 131,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "2", 654, 131,0  ,255,0);
				if (cell[2] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "3", 654, 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "3", 654, 16 ,0  ,255,0);
				if (cell[3] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "4", 481, 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "4", 481, 16 ,0  ,255,0);
				if (cell[4] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "5", 328, 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "5", 328, 16 ,0  ,255,0);
				if (cell[5] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "6", 176, 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "6", 176, 16 ,0  ,255,0);
				if (cell[6] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "7", 22 , 16 ,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "7", 22 , 16 ,0  ,255,0);
				if (cell[7] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "8", 22 , 132,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "8", 22 , 132,0  ,255,0);
				if (cell[8] == 2)Font_Lcd_Show(50, 125, 95, 62, 47, "9", 22 , 247,255,255,0);
				else Font_Lcd_Show(50, 125, 95, 62, 47, "9", 22 , 247,0  ,255,0);
				Font_Lcd_Show(30, 125, 95, 25, 40, "操作页面",22,362,0,255,0); 				
 			}
		}
		else if(page==page_photo) 
		{

			if(ts_x>0  && ts_x<74 && ts_y>0 && ts_y<74 && ts_touch==1)
			{
				page=page_set;
				sleep(1);
				Bmp_Show(0, 0, 800, 480, "./set.bmp");
			}
		}
	}
	pthread_exit(NULL);
	return (void *)0;
}


//1、打开触摸屏
void Ts_Open(void)
{
	ts_fd = open("/dev/input/event0", O_RDONLY);
	if (-1 == ts_fd){
		perror("open ts_fd error!\n");
		return ;
	}

	return ;
}

//2、获取触摸屏的x和y轴的值、触摸值
void Ts_Get_xy(int *ts_x, int *ts_y, int *ts_touch)
{
	//1、获取触摸屏的x和y轴坐标值、触摸值
	struct input_event ts_buf;					//输入子系统模型信息结构体

	while(1)
	{

		bzero(&ts_buf, sizeof(ts_buf));			//清空结构体buf
		read(ts_fd, &ts_buf, sizeof(ts_buf));	//堵塞在这里，直到有人摸触摸屏为止
		//获取x轴的值
		if(ts_buf.type == EV_ABS && ts_buf.code == ABS_X)
		{
			*ts_x = ts_buf.value/1024.0*800;		//黑色边沿的开发板
		
		}
		//获取y轴的值
		if(ts_buf.type == EV_ABS && ts_buf.code == ABS_Y)
		{
			*ts_y = ts_buf.value/600.0*480;		//黑色边沿的开发板
	
		}
		//获取触摸值
		if(ts_buf.type == EV_KEY && ts_buf.code == BTN_TOUCH)
		{
			*ts_touch =  ts_buf.value;
			break;	
		}
	}
	printf("(%d, %d, %d)\n",  *ts_x, *ts_y, *ts_touch);
		

	return ;

}


//3、关闭触摸屏
void Ts_Close(void)
{
	int ret_c = close(ts_fd);
	if (-1 == ret_c){
		perror("close ts_fd error!\n");
		return ;
	}
	return ;
}
